﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SG.Domain.Proveedor>" %>
<%@ Import Namespace="SG.Logic" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Crear proveedor
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="TitlePage" runat="server">
	Crear proveedor
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="HeaderLink" runat="server">
<%: Html.ActionLink("Ir al listado", "Index") %>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Head" runat="server">
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxwindows.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/skins/skyblue.css") %>"  rel="stylesheet" type="text/css" />
    
    
    
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.css") %>" rel="stylesheet" type="text/css" />   
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid_skins.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/skins/dhtmlxgrid_dhx_blue.css") %>" rel="stylesheet" type="text/css" />


    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxcommon.js") %>" type="text/javascript"></script>
     <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgridcell.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_filter.js") %>" type="text/javascript"></script>
    
    

    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxcommon.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxwindows.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxcontainer.js") %>" type="text/javascript"></script>    
    

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        <fieldset>
            <legend>Artículo</legend>
            <table class="tableData">
            <colgroup>
                <col width="200px" />
                 <col width="300px" />
            </colgroup>
               
               
                <tr>
                    <td class="label">Nombre</td>
                    <td class="data"><%: Html.TextBoxFor(model => model.Nombre, new { @style = "width:100%" })%></td>
                </tr>
                <tr>
                    <td class="label">CUIT</td>
                    <td class="data"><%: Html.TextBoxFor(model => model.CUIT, new { @style = "width:100%" })%></td>
                </tr>
                <tr>
                    <td class="label">Domicilio</td>
                    <td class="data"><%: Html.TextBoxFor(model => model.Domicilio, new { @style = "width:100%" })%></td>
                </tr>
                <tr>
                    <td class="label">Telefono</td>
                    <td class="data"><%: Html.TextBoxFor(model => model.Telefono, new { @style = "width:100%" })%></td>
                </tr>
                <tr>
                    <td class="label">Tiempo R</td>
                    <td class="data"><%: Html.TextBoxFor(model => model.TiempoR, new { @style = "width:100%" })%></td>
                </tr>
                

            </table>
            
                
         
        </fieldset>




            <fieldset>
            <legend>Artículos del Proveedor</legend>
                <input type="button" value="Agregar" onclick="winx.show();$('#updateRow').val('');" />
                <input type="button" value="Eliminar" onclick="removeRows();"/>
            
            <div id="gridList" style="height:100%"></div>
            
            
            
        </fieldset>
        
         <input type="button" onclick="save()" value="Guardar" />
          <div id="divSave"></div>
    <% } %>
  <%
      
       string array = "";
       foreach (var item in Model.ArticuloProveedor)
       {
           array += ",['0','"+
                    item.IdArticulo + "','" +
                    item.Articulo.Nombre+"','"+
                    item.TiempoEspera+"','"+                    
                    item.CostoAdquisicion + "','" +
                    item.CostoEmision+"','"+                    
                    item.Descuento+"','"+
                    item.CantidadMinDescuento+
                    "']";
                       
       }
       if (string.IsNullOrEmpty(array))
           array = "Array()";
       else 
           array =  "["+array.Substring(1)+"]";
       
        %>
      <div id="winVP" style="border: #cecece 1px solid; margin: 10px; text-align: center;">
     
         <table class="tableData" width="97%">
            <colgroup>
                <col width="30%" />
                 <col width="60%" />
            </colgroup>
               
                <tr>
                    <td class="label">Artículo</td>
                     <td class="data"><%: Html.DropDownList("IdArticuloCombo", new SelectList(new SG.Logic.ExpArticulo().GetAllArticulo(), "Id", "Nombre"), new { @style="width:100%"})%></td>
                </tr>
               
                <tr>
                    <td class="label">Tiempo de espera</td>
                    <td class="data"><input id="tiempoEspera" type="text" value="" style="width:100%" /></td>
                </tr>
                <tr>
                    <td class="label">Costo adquisición</td>
                    <td class="data"><input id="costoAdquisicion" type="text" value="" style="width:100%" /></td>
                </tr>
                <tr>
                    <td class="label">Costo emisión</td>
                    <td class="data"><input id="costoEmision" type="text" value="" style="width:100%" /></td>
                </tr>
                <tr>
                    <td class="label">Descuento</td>
                    <td class="data"><input id="descuento" type="text" value="" style="width:100%" /></td>
                </tr>
                 <tr>
                    <td class="label">Cantidad mínima descuento</td>
                    <td class="data"><input id="cantMinDesc" type="text" value="" style="width:100%" /></td>
                </tr>
            </table>
            <input type="hidden" id="updateRow" value=""/>
            <input type="button" onclick="addArticulo()" value="Aceptar" />
      </div>
    <script type="text/javascript">
  

        var jarray = <%=(array??"new array()") %>;

        var mygrid;
      
          var winx; 
          window.onload=doOnLoad;
        function doOnLoad(){
           
            mygrid = new dhtmlXGridObject('gridList');
            mygrid.setImagePath("<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/imgs/") %>");
            mygrid.setHeader(",,Artículo,Tiempo de espera,Costo adquisición,Costo emisión,Descuento,Cantidad mínima descuento");
            mygrid.attachHeader("<span style='margin-left:-10px;width:100%;text-align:center'><input id='checkAllGrid' type='checkbox' onclick='checkAll(this);'></span>, ,#select_filter,#numeric_filter,#numeric_filter,#numeric_filter,#numeric_filter,#numeric_filter");
            mygrid.setInitWidths("40,0,*,120,120,120,120,120");
            mygrid.setColAlign("center,left,left,left,left,left,left");
            mygrid.setColTypes("ch,ed,ed,ed,ed,ed,ed,ed");
            mygrid.setColumnIds("CHECK,IDARTICULOPROVEEDOR,ARTICULO,TIPODEESPERA,COSTO,SOTOEMISION,DESCUENTO,CANTIDADMINIMADESCUENTO");
            mygrid.enableEditEvents(false,false,false);
            mygrid.setSkin("light");
            mygrid.setColSorting("int,str,str,str,str,str,str,str");
            
            mygrid.enableAutoHeight(true);
            mygrid.attachEvent("onRowDblClicked", function(rId,cInd){
                
                
                $("#IdArticuloCombo option:selected").val(mygrid.cells(rId,1).getValue());                
                $("#IdArticuloCombo option:selected").text(mygrid.cells(rId,2).getValue());       
                $("#tiempoEspera").val(mygrid.cells(rId,3).getValue())
                $("#costoAdquisicion").val(mygrid.cells(rId,4).getValue());
                $("#costoEmision").val(mygrid.cells(rId,5).getValue());
                $("#descuento").val(mygrid.cells(rId,6).getValue());
                $("#cantMinDesc").val(mygrid.cells(rId,7).getValue());
                $("#updateRow").val(rId);
                winx.show();
            });  

            
            mygrid.init();
            mygrid.parse(jarray, "jsarray");




            
            var dhxWins= new dhtmlXWindows();
            dhxWins.setImagePath("<%=Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/imgs/") %>");
            
            
            winx = dhxWins.createWindow("wind", 200, 100, 400, 330);
            winx.setText("Editar");
            winx.hide();
            winx.attachObject("winVP");
            dhxWins.attachEvent("onClose", function(win){
               winx.hide();
               return false;
            });
          
           
        }
       
        function addArticulo() {
            if(  $("#tiempoEspera").val()=="" || $("#costoAdquisicion").val()=="" || $("#costoEmision").val()=="" || $("#descuento").val()=="" ||$("#cantMinDesc").val()==""){
                alert("Debe ingresar todos los datos");
            
                return;
            }

             var searchResult=mygrid.findCell($("#IdArticuloCombo option:selected").val(),1,true);
             if(searchResult.length>0 && $("#updateRow").val()!=searchResult[0][0]){
                alert("El producto ya está vinculado al proveedor");
                return;
             }
           if($("#updateRow").val()==""){
               mygrid.addRow(mygrid.uid(),"0,"+$("#IdArticuloCombo option:selected").val()+","+$("#IdArticuloCombo option:selected").text()+","+$("#tiempoEspera").val()+","+$("#costoAdquisicion").val()+","+$("#costoEmision").val()+","+$("#descuento").val()+","+$("#cantMinDesc").val());
            }else{
                mygrid.cells($("#updateRow").val(),1).setValue($("#IdArticuloCombo option:selected").val());
                mygrid.cells($("#updateRow").val(),2).setValue($("#IdArticuloCombo option:selected").text());
                mygrid.cells($("#updateRow").val(),3).setValue($("#tiempoEspera").val());
                mygrid.cells($("#updateRow").val(),4).setValue($("#costoAdquisicion").val());
                mygrid.cells($("#updateRow").val(),5).setValue($("#costoEmision").val());
                mygrid.cells($("#updateRow").val(),6).setValue($("#descuento").val());
                mygrid.cells($("#updateRow").val(),7).setValue($("#cantMinDesc").val());
            }

            winx.hide();
        }
        function removeRows() {
            var xx=mygrid.getAllRowIds(",");
            xx= xx.split(",");
            for(i=0;i<xx.length;i++){
                if(mygrid.cells(xx[i],0).getValue(0)==1)
                    mygrid.deleteRow(xx[i]);
            }
        }
         function checkAll(obj){
            window.event.cancelBubble = true;
            var xx=mygrid.getAllRowIds(",");
            xx= xx.split(",");
            for(i=0;i<xx.length;i++){
                 mygrid.cells(xx[i],0).setValue(obj.checked?1:0);
            }
        }
        function save(){
            var hidden="";
            mygrid.forEachRow(function(id){
             hidden+="<input type='hidden' name='IdArticulo'                value='"+mygrid.cells(id,1).getValue()+"'/>"; 
             hidden+="<input type='hidden' name='nombreArticulo'            value='"+mygrid.cells(id,2).getValue()+"'/>";
             hidden+="<input type='hidden' name='TiempoEspera'              value='"+mygrid.cells(id,3).getValue()+"'/>";
             hidden+="<input type='hidden' name='CostoAdquisicion'          value='"+mygrid.cells(id,4).getValue()+"'/>";
             hidden+="<input type='hidden' name='CostoEmision'              value='"+mygrid.cells(id,5).getValue()+"'/>";
             hidden+="<input type='hidden' name='Descuento'                 value='"+mygrid.cells(id,6).getValue()+"'/>";
             hidden+="<input type='hidden' name='CantidadMinimaDescuento'   value='"+mygrid.cells(id,7).getValue()+"'/>";
                        
                        
            });
            $("#divSave").html(hidden); 

            document.forms[0].submit();
        }

    
    </script>
</asp:Content>

